﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Security.Cryptography;
using System.Text;
using System.Web.Mail;
using DevExpress.Office.Utils;
using TheHeGeoDao;
using MailMessage = System.Net.Mail.MailMessage;

namespace TheHeGeo.Util
{
    public class UserHelper
    {
        public static string Sha256Encrypt(string phrase)
        {
            var encoder = new UTF8Encoding();
            var sha256Hasher = new SHA256Managed();
            byte[] hashedDataBytes = sha256Hasher.ComputeHash(encoder.GetBytes(phrase));
            return ByteArrayToString(hashedDataBytes);
        }

        public static string GenUserPid(string user)
        {
            return Sha256Encrypt(user);
        }

        public static string UserPwdEncrypt(string pwd)
        {
            return pwd;
            //return Sha256Encrypt(Sha256Encrypt(pwd));
        }

        public static bool CheckFollowPwdEncrypt(string pwd,string ma)
        {
            return true;
        }

        public static string ByteArrayToString(byte[] inputArray)
        {
            var output = new StringBuilder("");
            foreach (byte t in inputArray)
            {
                output.Append(t.ToString("X2"));
            }
            return output.ToString();
        }

        public static bool CheckQuyen(string us, int maDa)
        {
            var acc  = DaoHelper.Entities.PhanQuyenDuAns.FirstOrDefault(p => p.MaDuAn == maDa && p.UserName == us);
            if (acc == null)
                return false;
            return true;
        }
        public static void SendEmail(List<string> to,
                                    string form,string subj,
                                    string body,string host,int port,
                                    string userName,string passw)
        {
            try
            {
                var mail = new MailMessage();
                foreach (var t in to)
                {
                    mail.To.Add(t);
                }
                mail.From = new MailAddress(form);
                mail.Subject = subj;
                mail.Body = body;
                mail.IsBodyHtml = true;

                var smtp = new SmtpClient();
                smtp.Host = host; //Or Your SMTP Server Address,"smtp.gmail.com"
                smtp.Credentials = new NetworkCredential(userName, passw); //Or your Smtp Email ID and Password
                smtp.EnableSsl = true;
                smtp.Port = port; //587; //If not work, you can use 25 here....
                smtp.Send(mail);
            }
            catch (Exception)
            {
                
            }
        }
    }
}